import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import 'package:get/get.dart';
import 'package:kq_flutter_core_widget/utils/kq_multi_click_util.dart';
import '../../resources/kq_theme_colors.dart';
import '../../resources/l10n/kq_intl.dart';
import 'package:kq_flutter_core_widget/widgets/button/kq_ink_well.dart';

import '../../theme/kq_theme_manager.dart';
import '../divider/kq_divider.dart';

/// 通用确认对话框
///
/// @author 周卓
///
/// 顶部标题 + 中间消息 + 底部2个按钮
class KqConfirmDialog extends AlertDialog {
  /// 标题
  final String? titleString;

  /// 标题居中方式
  final TextAlign? titleAlign;

  /// 消息
  final String msg;

  /// 消息对齐方式，不指定则默认居中，超过一行左对齐
  final TextAlign? msgTextAlign;

  /// 左边按钮文字，默认"取消"
  final String? leftBtnText;

  /// 左边按钮文字，默认"确定"
  final String? rightBtnText;

  /// 左边按钮回调
  final Function()? onLeftBtnTap;

  /// 右边按钮回调
  final Function()? onRightBtnTap;

  /// 标题文字颜色
  final Color? titleColor;

  /// 消息文字颜色
  final Color? msgColor;

  /// 左边按钮文字颜色
  final Color? leftBtnColor;

  /// 右边按钮文字颜色
  final Color? rightBtnColor;

  /// 标题文字大小
  final double? titleFontSize;

  /// 消息文字大小
  final double? msgFontSize;

  /// 按钮文字
  final double? btnFontSize;

  /// 标题文字字重
  final FontWeight? titleFontWeight;

  /// 自定义内容
  final Widget Function()? msgCustomWidget;

  const KqConfirmDialog({
    Key? key,
    this.titleString,
    this.titleAlign,
    required this.msg,
    this.msgTextAlign,
    this.leftBtnText,
    this.rightBtnText,
    this.titleColor,
    this.leftBtnColor,
    this.rightBtnColor,
    this.msgColor,
    this.titleFontSize,
    this.msgFontSize,
    this.btnFontSize,
    this.onLeftBtnTap,
    this.onRightBtnTap,
    this.titleFontWeight,
    this.msgCustomWidget,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    bool isLangScape = context.orientation == Orientation.landscape;

    final List<Widget> children = <Widget>[];

    /// 上边距
    children.add(SizedBox(
      width: 1.r,
      height: 24.r,
    ));

    /// 标题
    if (titleString != null) {
      children.add(Container(
        padding: EdgeInsets.symmetric(horizontal: 16.r),
        alignment: Alignment.center,
        child: Text(
          titleString!,
          textAlign: titleAlign ?? TextAlign.left,
          style: TextStyle(
              fontSize: titleFontSize ?? 16.sp,
              color: titleColor ??
                  KqThemeManager.instance.getConfig().dialogConfig.titleColor ??
                  KqThemeColors.text26,
              fontWeight: titleFontWeight ?? FontWeight.w600),
        ),
      ));
      children.add(SizedBox(
        width: 1.r,
        height: 16.r,
      ));
    }

    if (msgCustomWidget != null) {
      children.add(msgCustomWidget!.call());
    }

    /// 消息
    children.add(LimitedBox(
      maxHeight: isLangScape ? 250.r : 500.r,
      child: SingleChildScrollView(
        child: Container(
          margin: EdgeInsets.symmetric(horizontal: 16.r),
          padding: EdgeInsets.symmetric(horizontal: 8.r),
          alignment: msgTextAlign == null || msgTextAlign == TextAlign.center
              ? Alignment.center
              : msgTextAlign == TextAlign.right
                  ? Alignment.centerRight
                  : Alignment.centerLeft,
          child: Text(
            msg,
            textAlign: msgTextAlign,
            style: TextStyle(
                fontSize: msgFontSize ?? 16.sp,
                color: msgColor ??
                    KqThemeManager.getDialogConfig().msgColor ??
                    KqThemeColors.text59),
          ),
        ),
      ),
    ));

    /// 下边距
    children.add(SizedBox(
      width: 1.r,
      height: 24.r,
    ));

    /// 底部按钮
    children.add(const KqDivider());

    children.add(Row(
      children: [
        Expanded(
            child: KqInkWell(
          onTap: () {
            if (KqMultiClickUtil.isMultiClick()) {
              return;
            }
            if (onLeftBtnTap != null) {
              onLeftBtnTap!();
            }
          },
          child: Container(
            alignment: Alignment.center,
            width: double.infinity,
            height: 48.r,
            child: Text(
              leftBtnText ?? KqIntl.currentResource.cancel,
              style: TextStyle(
                  fontSize: 16.sp,
                  color: leftBtnColor ?? KqThemeManager.instance
                          .getConfig()
                          .dialogConfig
                          .cancelBtnColor ??
                      KqThemeColors.text8C),
            ),
          ),
        )),
        KqVerticalDivider(
          height: 48.r,
        ),
        Expanded(
            child: KqInkWell(
          onTap: () {
            if (KqMultiClickUtil.isMultiClick()) {
              return;
            }
            if (onRightBtnTap != null) {
              onRightBtnTap!();
            }
          },
          child: Container(
            alignment: Alignment.center,
            width: double.infinity,
            height: 48.r,
            child: Text(
              rightBtnText ?? KqIntl.currentResource.confirm,
              style: TextStyle(
                  fontSize: 16.sp,
                  color: rightBtnColor ?? KqThemeManager.instance
                          .getConfig()
                          .dialogConfig
                          .mainBtnColor ??
                      KqThemeColors.textLightBlue),
            ),
          ),
        ))
      ],
    ));

    Widget dialogChild = Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: children,
    );
    return UnconstrainedBox(
      child: SizedBox(
        width: isLangScape ? 500.r : 320.r,
        child: Material(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.all(
              Radius.circular(
                  KqThemeManager.instance.getConfig().dialogConfig.radius ??
                      8.r),
            ),
          ),
          color: KqThemeManager.instance.getConfig().dialogConfig.bgColor ??
              KqThemeColors.bgWhite,
          child: ClipRRect(
            borderRadius: BorderRadius.all(
              Radius.circular(
                  KqThemeManager.instance.getConfig().dialogConfig.radius ??
                      8.r),
            ),
            child: dialogChild,
          ),
        ),
      ),
    );
  }
}
